'Copyright (c) 2009 Grailtek

'Permission is hereby granted, free of charge, to any person
'obtaining a copy of this software and associated documentation
'files (the "Software"), to deal in the Software without
'restriction, including without limitation the rights to use,
'copy, modify, merge, publish, distribute, sublicense, and/or sell
'copies of the Software, and to permit persons to whom the
'Software is furnished to do so, subject to the following
'conditions:

'The above copyright notice and this permission notice shall be
'included in all copies or substantial portions of the Software.

'THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
'EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
'OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
'NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
'HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
'WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
'FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
'OTHER DEALINGS IN THE SOFTWARE.

Imports System.Xml.Serialization
Imports System.Reflection
Imports System.Text
Imports System.Aspect

<Serializable()> _
Public Structure Contract
    Implements IEquatable(Of Contract)

    Public Const BindingFlags As BindingFlags = BindingFlags.Instance Or BindingFlags.NonPublic Or BindingFlags.Public

    Public MethodUIName, AspectID, [Delegate] As String
    Public Assembly As AssemblyInfo
    Public Generated As GeneratedInfo
    Public Inputs As InputSignature
    Public Outputs As OutputSignature

    Public ReadOnly Property IsEmpty() As Boolean
        Get
            Return Me.MethodUIName Is Nothing
        End Get
    End Property

    Public Sub New(ByVal Aspect As IAspect)
        MethodUIName = Aspect.UIName
        AspectID = Aspect.AspectID.ToString
        [Delegate] = Aspect.DelegateInfo.DeclaringDelegate.FullName.Replace("+", ".")
        Dim d As DelegateInfo = Aspect.DelegateInfo
        Assembly = New AssemblyInfo(Aspect)
        Dim inputParameters As Parameter() = d.InputMappings.Select(Of Parameter)(Function(i As InputMapping) New Parameter(i)).ToArray
        Inputs = New InputSignature(inputParameters)
        Dim outputParameters As Parameter() = d.OutputMappings.Select(Of Parameter)(Function(o As OutputMapping) New Parameter(o)).ToArray
        Outputs = New OutputSignature(outputParameters, d.ReturnTypeIsArray)
        Generated = New GeneratedInfo(Aspect)
    End Sub

    Private Class Serializer
        Inherits Xml.Serialization.XmlSerializer
        Private Sub New()
            MyBase.new(GetType(Contract), New XmlRootAttribute(GetType(Contract).Name))
        End Sub
        Private Shared ReadOnly StaticInstance As New Serializer
        'NOTE: using a MemberwiseClone is extremely important for performance - we only want
        'the constructor called once as it is *very* performance hungry - every time it's called, code is
        'generated in a temporary folder and compiled!
        Public Shared Function GetNew() As Serializer
            Return DirectCast(StaticInstance.MemberwiseClone, Serializer)
        End Function
    End Class

    Public Function Serialize() As String
        Dim s As Serializer = Serializer.GetNew
        Dim sb As New StringBuilder
        Using sw As New System.IO.StringWriter(sb)
            s.Serialize(sw, Me)
        End Using
        Return sb.ToString()
    End Function

    Public Shared Function Deserialize(ByVal text As String) As Contract
        Dim ret As Contract = Nothing
        If Not text Is Nothing Then
            If text.Contains(String.Format("</{0}>", GetType(Contract).Name)) Then
                Using sr As New IO.StringReader(text)
                    Dim o As Object = Serializer.GetNew.Deserialize(sr)
                    ret = DirectCast(o, Contract)
                End Using
            End If
        End If
        Return ret
    End Function

    Public Overloads Function Equals(ByVal other As Contract) As Boolean Implements System.IEquatable(Of Contract).Equals
        Dim ret As Boolean = True
        ret = ret AndAlso Me.Assembly.Equals(other.Assembly)
        ret = ret AndAlso Me.Inputs.Equals(other.Inputs)
        ret = ret AndAlso Me.Outputs.Equals(other.Outputs)
        ret = ret AndAlso Me.MethodUIName = other.MethodUIName
        ret = ret AndAlso Me.AspectID = other.AspectID
        ret = ret AndAlso Me.[Delegate] = other.[Delegate]
        Return ret
    End Function

End Structure
